package com.example.seh;


import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.app.Activity;
import android.graphics.drawable.Drawable;

import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;

import android.view.WindowManager;
import android.widget.ImageView;

import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.seh.add.PubActivity;
import com.example.seh.chat.ChatFragment;
import com.example.seh.home.HomeFragment;
import com.example.seh.person.LoginListener;
import com.example.seh.person.ListenerManager;
import com.example.seh.person.MeFragment;
import com.example.seh.person.PayListener;
import com.example.seh.square.SquareFragment;
import com.example.seh.util.FragmentAdapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity{
    private RadioButton mRbHome,mRbPond,mRbPerson,mRbMsg;
    private RadioGroup mRbGroup;
    private ImageView mIvAdd;

    private ViewPager2 vp2;

    private HomeFragment homeFragment = new HomeFragment();
    private SquareFragment squareFragment = new SquareFragment();
    private ChatFragment chatFragment = new ChatFragment();
    private MeFragment meFragment = new MeFragment();
    private FragmentAdapter adapter;
    List<Fragment> fragments = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_main);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
        initViews();
        getData();
        adapter = new FragmentAdapter(fragments, this);
        vp2.setAdapter(adapter);
        vp2.setUserInputEnabled(false);
        vp2.setOffscreenPageLimit(4);
        initListener();
        mRbHome.setChecked(true);

        getLoginOrExitData();
        getPayResultData();
    }

    private void getData() {
        fragments.add(homeFragment);
        fragments.add(squareFragment);
        fragments.add(chatFragment);
        fragments.add(meFragment);
    }

    private void initListener() {
        mRbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_home:
                        vp2.setCurrentItem(0);
                        break;
                    case R.id.rb_pond:
                        vp2.setCurrentItem(1);
                        break;
                    case R.id.rb_message:
                        vp2.setCurrentItem(2);
                        break;
                    case R.id.rb_me:
                        vp2.setCurrentItem(3);
                        break;
                    default:
                        break;
                }
            }
        });

        mIvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PubActivity.show(MainActivity.this);
            }
        });

    }

    private void initViews() {
        mRbHome = findViewById(R.id.rb_home);
        mRbPond = findViewById(R.id.rb_pond);
        mRbMsg = findViewById(R.id.rb_message);
        mRbPerson = findViewById(R.id.rb_me);
        mIvAdd = findViewById(R.id.rbAdd);
        mRbGroup = findViewById(R.id.radioGroup);
//        mTVDes = findViewById(R.id.tvDes);
        vp2 = findViewById(R.id.vp2);

        Drawable dbHome = getResources().getDrawable(R.drawable.selector_home);
        dbHome.setBounds(0, 0, 40, 40);
        mRbHome.setCompoundDrawables(null, dbHome, null, null);

        Drawable dbPond = getResources().getDrawable(R.drawable.selector_pond);
        dbPond.setBounds(0, 0, 40, 40);
        mRbPond.setCompoundDrawables(null, dbPond, null, null);

        Drawable dbMsg = getResources().getDrawable(R.drawable.selector_message);
        dbMsg.setBounds(0, 0, 40, 40);
        mRbMsg.setCompoundDrawables(null, dbMsg, null, null);

        Drawable dbMe = getResources().getDrawable(R.drawable.selector_person);
        dbMe.setBounds(0, 0, 40, 40);
        mRbPerson.setCompoundDrawables(null, dbMe, null, null);
    }

    private void getLoginOrExitData() {
        ListenerManager informUpdateListenerManager = new ListenerManager(MainActivity.this);
        informUpdateListenerManager.getInstance(MainActivity.this).setLoginListener(new MyLoginListener(this));
    }
    private void getPayResultData() {
        ListenerManager informUpdateListenerManager = new ListenerManager(MainActivity.this);
        informUpdateListenerManager.getInstance(MainActivity.this).setPayListener(new MyPayListener(this));
    }
    //静态内部类实现接口监听
    class MyPayListener implements PayListener {
        private WeakReference<Activity> mWeakReference;
        public MyPayListener(Activity activity){
            mWeakReference = new WeakReference<Activity>(activity);//使用弱引用引用外部Activity对象
        }


        @Override
        public void payAndUpdata() {
//            homeFragment.onResume();
        }
    }
    class MyLoginListener implements LoginListener {
        private WeakReference<Activity> mWeakReference;
        public MyLoginListener(Activity activity){
            mWeakReference = new WeakReference<Activity>(activity);//使用弱引用引用外部Activity对象
        }


        @Override
        public void updata() {
            Log.e("chatlogin","登录");
            chatFragment.onResume();
            meFragment.onResume();
            Log.e("ex","ddd");
//            Bundle bundle = new Bundle();
//            bundle.putBoolean("isEdit", false);
//            chatFragment.setArguments(bundle);
        }
    }


//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == 101) {
//            Log.e("login","login");
//            meFragment.onResume();
//            chatFragment.onResume();
//        }
//    }
}